<?php
if(get_magic_quotes_gpc()){ die('Magic Quotes activated.'); }

ob_start();
session_start();
header('Content-type: text/html; charset=UTF-8');

/* Manage language session */
if(!isset($_COOKIE['lang']) || !isset($_SESSION['lang'])){
	$_SESSION['lang'] = substr(strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2);
}

/* 'dev' or 'prod' */
define('APP_USE_TYPE', 'dev');

error_reporting(E_ALL);
date_default_timezone_set('Europe/Paris');

// Include of configuration file
require_once dirname(__FILE__).'/config/config.php';

require_once WEBAPP_DIR.'/application/function/protectScript.php';
require_once WEBAPP_DIR.'/application/function/displayContentArray.php';

// Inclusion des classes et initialisation
require_once MODEL_DIR.'/Mysqli.php';
try{
	$oMysqliInstance = AYSMysqli::getInstance();
}catch(MysqliConnexionException $oException){
	$oException->addLog('MysqliConnexion');
	$sMessage = $oException->getMessage();
	include_once VIEW_DIR.'/errors.php';
	die();
}

set_include_path(
	'.'.
	PATH_SEPARATOR.
	MODEL_DIR.
	PATH_SEPARATOR.
	get_include_path()
);

require_once WEBAPP_DIR.'/application/Exceptions/ExceptionsList.php';

/* Display */
include_once CONTROLLER_DIR.'/headerController.php';

try{
	echo '<script type="text/javascript" src="'.SCRIPT_DIR.'/js/functionDisplayDirectory.js"></script>';
	echo '<div id="content"><ul>';

	require_once MODEL_DIR.'/SourceEditorModel.php';
	$oEditorInstance = new SourceEditorModel(array('sCurrentDirectory' => '../'));

	$asaDirectoryContent = SourceEditorModel::listCurrentDirectory();
	foreach($asaDirectoryContent['aisDirectoryInfos'] AS $sDirectoryName){
		echo '
			<li id="'.$sDirectoryName.'">
				<img src="'.IMAGE_DIR.'/folder.gif" alt="folder" />
				<a href="#'.$sDirectoryName.'" onClick="javascript:displayDirectory(\''.$sDirectoryName.'\');">
					'.$sDirectoryName.'
				</a>
			</li>'
		;
	}

	foreach($asaDirectoryContent['aiassFilesInfos'] AS $iFile){
		switch($iFile['sExtensionFile']){
			case 'html':
			case 'xhtml':
			case 'phtml':
				$sImage = 'page_html.gif';
				break;

			case 'php':
				$sImage = 'page_php.gif';
				break;

			case 'java':
				$sImage = 'page_java.gif';
				break;

			case 'xml':
			case 'xsl':
			case 'dtd':
			case 'js':
				$sImage = 'page_script.gif';
				break;

			default:
				$sImage = 'page_text.gif';
				break;
		}
		echo '
			<li>
				<img src="'.IMAGE_DIR.'/'.$sImage.'" alt="file" />
				<a href="displayFile.php?fileName='.$iFile['sCompleteNameFile'].'&amp;language='.$iFile['sExtensionFile'].'">
					'.$iFile['sCompleteNameFile'].'
				</a>
			</li>'
		;
	}
	displayContentArray('asaDirectoryContent', $asaDirectoryContent);

	echo '</ul></div>';
}catch(PageNotFoundException $oException){
	$oException->addLog('PageNotFound');
	$sMessage = $oException->getMessage();
	include_once VIEW_DIR.'/errors.php';
}catch(Exception $oException){
	$oException->addLog('Exception');
	$sMessage = $oException->getMessage();
	include_once VIEW_DIR.'/errors.php';
}


include_once CONTROLLER_DIR.'/footerController.php';

// Closing current connexion with database
$oMysqliInstance->close();

ob_end_flush();
?>
